<template>
  <a-modal
    title="修改通道信息"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel">

    <a-spin :spinning="formLoading">
      <a-form :form="form">

        <a-form-item
          style="display: none;"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-input v-decorator="['id']"/>
        </a-form-item>

        <a-form-item
          style="display: none;"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-input v-decorator="['gatewayId']"/>
        </a-form-item>

        <a-form-item
          label="通道"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-input placeholder="请输入通道" v-decorator="['channel', {rules: [{required: true, message: '请输入通道！'}]}]" />
        </a-form-item>

        <a-form-item
          label="安装位置"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-input placeholder="请输入安装位置" v-decorator="['installLocation', {rules: [{required: true, message: '请输入安装位置！'}]}]" />
        </a-form-item>

        <a-form-item
          label="监听设备"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-select style="width: 100%" placeholder="请选择监听设备" v-decorator="['deviceId', {rules: [{ required: true, message: '请选择监听设备(变压器）！' }]}]" >
            <a-select-option v-for="(item,index) in groupCodeList" :key="index" :value="item.id" >{{ item.deviceName }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          label="传感器类型"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-select style="width: 100%" placeholder="请选择传感器类型" v-decorator="['channelType', {rules: [{ required: true, message: '请选择传感器类型！' }]}]" >
            <a-select-option v-for="(item,idx) in channelTypeList" :key="idx" :value="item.id" >{{ item.name }}</a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item
          label="侦听点位"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          v-if="isFos"
        >
          <a-input-number placeholder="侦听点位" v-decorator="['startPoint', {rules: [{required: true, message: '请输入侦听点位！'}]}]" :min="200" />
        </a-form-item>

<!--        <a-form-item-->
<!--          label="结束点位"-->
<!--          :labelCol="labelCol"-->
<!--          :wrapperCol="wrapperCol"-->
<!--          v-if="isFos"-->
<!--        >-->
<!--          <a-input-number placeholder="结束点位" v-decorator="['endPoint', {rules: [{required: true, message: '请输入结束点位！'}]}]" />-->
<!--        </a-form-item>-->

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { edit } from '@/api/modular/main/bizgatewaychannel/bizGatewayChannelManage'
  import { deviceList } from '@/api/modular/main/bizdevice/bizDeviceManage'
  import { channelTypeList } from '@/api/modular/main/bizchanneltype/bizChannelTypeManage'

  export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        visible: false,
        confirmLoading: false,
        formLoading: false,
        form: this.$form.createForm(this),
        groupCodeList: {},
        channelTypeList: [],
        isFos: false
      }
    },
    methods: {
      edit (record, stationId) {
        const that = this
        that.visible = true
        that.formLoading = true
        deviceList({ stationId: stationId }).then(res => {
          that.groupCodeList = res.data
        })
        channelTypeList().then(res => {
          if (res.success) {
            that.channelTypeList = res.data
            that.formLoading = false
          }
        })

        if (record.channelTypeCode === 'ZKHY-FOS-001') {
          this.isFos = true
          setTimeout(() => {
            that.form.setFieldsValue({
              startPoint: record.startPoint
              // endPoint: record.endPoint
            })
          }, 100)
        }

        setTimeout(() => {
          that.form.setFieldsValue({
            id: record.id,
            channel: record.channel,
            installLocation: record.installLocation,
            deviceId: record.deviceId,
            gatewayId: record.gatewayId,
            channelType: Number(record.channelType)
          })
        }, 100)
      },
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true

        validateFields((errors, values) => {
          if (!errors) {
            edit(values).then((res) => {
              this.confirmLoading = false
              if (res.success) {
                this.$message.success('编辑成功')
                this.handleCancel()
                this.$emit('ok', values)
              } else {
                this.$message.info('编辑失败：' + res.message)
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      handleCancel () {
        this.form.resetFields()
        this.isFos = false
        this.visible = false
      }
    }
  }
</script>
